package com.hgy.crowd.handler;

import com.hgy.crowd.service.MemberService;
import com.hgy.crowd.po.MemberPO;
import com.hgy.crowd.util.ResultEntity;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Author He
 * @Date 2020/6/30 11:08
 */
@RestController
public class MemberHandler {
    @Resource
    private MemberService memberService;
    @RequestMapping("/get/member/by/account/remote")
    public ResultEntity<MemberPO> getMemberByLoginAccount(@RequestParam("account") String account){
        ResultEntity resultEntity = memberService.getMemberByLoginAccount(account);
        return resultEntity;
    }

    @RequestMapping("/save/member")
    public ResultEntity saveMember(@RequestBody MemberPO memberPO){
        try {
            memberService.saveMember(memberPO);
        }catch (Exception e){
            if (e instanceof DuplicateKeyException){
                return ResultEntity.failed("当前用户已存在");
            }else {
                return ResultEntity.failed(e.getMessage());
            }
        }
        return ResultEntity.successWithoutData();
    }


}
